package cn.edu.scau.cmi.xianjiehua.comprehension.dao;

import cn.edu.scau.cmi.xianjiehua.comprehension.domain.Tutor;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class TutorDao extends BaseHibernateDAO {
    private static final Logger log = LoggerFactory.getLogger(TutorDao.class);
    // property constants
    public static final String NAME = "name";

    public void save(Tutor transientInstance) {
        log.debug("saving Tutor instance");
        try {
        	Session session = getSession();
        	session.beginTransaction();
            session.save(transientInstance);
            session.getTransaction().commit();
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    public void delete(Tutor persistentInstance) {
        log.debug("deleting Tutor instance");
        try {
        	Session session = getSession();
        	session.beginTransaction();
        	session.delete(persistentInstance);
        	session.getTransaction().commit();
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public Tutor findById(Long id) {
        log.debug("getting Tutor instance with id: " + id);
        try {
            Tutor instance = getSession().get(Tutor.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    @SuppressWarnings("unchecked")
	public List<Tutor> findByExample(Tutor instance) {
        log.debug("finding Tutor instance by example");
        try {
            List<Tutor> results = getSession().createCriteria(Tutor.class)
                    .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    @SuppressWarnings("unchecked")
	public List<Tutor> findByProperty(String propertyName, Object value) {
        log.debug("finding Tutor instance with property: " + propertyName + ", value: " + value);
        try {
            String queryString = "from Tutor as model where model." + propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);
            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<Tutor> findByName(Object name) {
        return findByProperty(NAME, name);
    }

    @SuppressWarnings("unchecked")
	public List<Tutor> findAll() {
        log.debug("finding all Tutor instances");
        try {
            String queryString = "from Tutor";
            Query queryObject = getSession().createQuery(queryString);
            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public Tutor merge(Tutor detachedInstance) {
        log.debug("merging Tutor instance");
        try {
        	Session session = getSession();
        	session.beginTransaction();
            Tutor result = (Tutor) session.merge(detachedInstance);
        	session.getTransaction().commit();
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(Tutor instance) {
        log.debug("attaching dirty Tutor instance");
        try {
        	Session session = getSession();
        	session.beginTransaction();
        	session.saveOrUpdate(instance);
        	session.getTransaction().commit();
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public void attachClean(Tutor instance) {
        log.debug("attaching clean Tutor instance");
        try {
        	Session session = getSession();
        	session.beginTransaction();
            session.buildLockRequest(LockOptions.NONE).lock(instance);
        	session.getTransaction().commit();

            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}
